Option Explicit
' 0 errorlevel
' 1 src_repo_URL
' 2 src_file_URL
' 3 dst_workcopy_path
' 4 rev_number
' 5 type_of_change
' 6 dst_repo_URL
'OR
' "!" dst_repo_path

If WScript.Arguments.Count <> 7 And _
   WScript.Arguments.Count <> 2 And WScript.Arguments(0) <> "!" Then
        Dim sMsg, sArg
        sMsg="For internal purposes only" + vbCr
        For Each sArg in WScript.Arguments
          sMsg=sMsg+"["+sArg+"]"+vbCr
        Next
        MsgBox sMsg, vbInformation, Wscript.ScriptName
        WScript.Quit(1)
End If

Dim FSO, FileH
Set FSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 7 Then
    ProcessFile WScript.Arguments(0), WScript.Arguments(1), WScript.Arguments(2), _
                WScript.Arguments(3), WScript.Arguments(4), WScript.Arguments(5), _
                WScript.Arguments(6)
Else
    ConvertPath WScript.Arguments(1)
End If
WScript.Quit(0)


Function ProcessFile(errorlevel, src_repo_path, src_file_path, _
                     dst_workcopy_path, rev_number, type_of_change, dst_repo_URL)
    Dim sCopyName, sNum
    sNum = InStrRev(src_file_path, "(from /")
    sCopyName = ""
    If 0 <> sNum Then
        sCopyName = dst_repo_URL + Mid(src_file_path, sNum+Len("(from "))
        src_file_path = Mid(src_file_path, 1, sNum-2)
        sNum = InStrRev(sCopyName, ":")
        sNum = Mid(sCopyName, sNum+1, InStrRev(sCopyName, ")")-sNum-1)
        sCopyName = Mid(sCopyName, 1, Len(sCopyName)-1-Len(sNum)-1)
    End If
    src_file_path = src_repo_path + src_file_path
    
    Set FileH = FSO.OpenTextFile("z_.Bat", 2, True)
    FileH.WriteLine("@echo off")
    'FileH.WriteLine("echo #" + type_of_change + vbTab + _
    '                Mid(src_file_path, 1+1+Len(src_repo_path)))
    'If ""<>sCopyName FileH.WriteLine("echo From:" + vbTab + sCopyName + "@" + sNum)
    Dim sPart, sNewName
    sPart = Mid(src_file_path, 1+Len(src_repo_path))
    sNewName = dst_workcopy_path + Replace(sPart, "/", "\")
    sPart = Mid(sNewName, 1, InStrRev(sNewName, "\")-1)

    If     "0" <> errorlevel Then
        FileH.WriteLine("echo Heeeeeey! Broken REV here! && pause && exit>&2")
    ElseIf "D" =  type_of_change Then
        FileH.WriteLine("svn del --force """ + sNewName + """>NUL")
        If FSO.FolderExists(sNewName) Then FileH.WriteLine("rmdir /S /Q """ + sNewName + """")
    Else
        If Not FSO.FolderExists(sPart) Then FileH.WriteLine("md """ + sPart + """")
        If sCopyName <> "" Then
            FileH.WriteLine("svn copy """ + sCopyName + "@" + _
                            sNum + """ """ + sNewName + """>NUL")
            FileH.WriteLine("goto props")
        End If
        If Not FSO.FolderExists(sNewName) Then
            FileH.WriteLine("svn cat """ + src_file_path + "@" + _
                            rev_number + """>z_.dat 2>z_.log")
            FileH.WriteLine("for /F ""tokens=5,8"" %%i in (z_.log) do" + _
                            " if %%i==refers if %%j==directory goto dir")
            FileH.WriteLine("if %ERRORLEVEL%==0 (")
            FileH.WriteLine("copy /B /Y z_.dat """ + sNewName + """>NUL")
            FileH.WriteLine(") else (")
            FileH.WriteLine("if not exist """ + sNewName + """ (")
            FileH.WriteLine("echo.>""" + sNewName + """")
            If "1"<>rev_number And ""=sCopyName Then FileH.WriteLine("svn add --parents """ + sNewName + """>NUL")
            FileH.WriteLine(")")
            If "1"<>rev_number Then FileH.WriteLine("svn propset dirtycopy rev"+rev_number+" """ + sNewName + """>NUL")
            'FileH.WriteLine("echo ==== Broken file """ + sNewName + """ (marked as 'dirtycopy' by property)")
            FileH.WriteLine("goto props")
            FileH.WriteLine(")")          
            FileH.WriteLine(":fin")
            If 0 <> InStr(type_of_change, "A") Then ' file/dir was added
                If "1"<>rev_number And ""=sCopyName Then FileH.WriteLine("svn add --parents """ + sNewName + """>NUL")
            End If
            FileH.WriteLine("goto props")
            FileH.WriteLine(":dir")
            FileH.WriteLine("if not exist """ + sNewName + "\NUL"" md """ + sNewName + """")
            FileH.WriteLine("goto fin")
        End If
        FileH.WriteLine(":props")
        If "1"<>rev_number Then
            FileH.WriteLine("for /F ""usebackq skip=1"" %%p in (`svn proplist """ + src_file_path + _
                            "@" + rev_number + """`) do (")
            FileH.WriteLine("svn propget %%p """ + src_file_path + "@" + rev_number + """>z_.dat")
            'Mute error output for ignoring warning message "svn: Pathname not terminated by ':'"
            FileH.WriteLine("svn propset %%p """ + sNewName + """ -F z_.dat --force>NUL 2>NUL")
            FileH.WriteLine(")")
        End If
    End If
    FileH.Close
    'If 0 <> InStr(type_of_change, "A") Then MsgBox sNewName, vbInformation, type_of_change
End Function

Function ConvertPath(dst_repo_path)
    Set FileH = FSO.OpenTextFile("z_.dat", 8, True)
    FileH.WriteLine("file:///" + Replace(dst_repo_path, "\", "/"))
    FileH.Close
End Function